//
//  TotalDeviceSelectedViewController.m
//  EZ Tracker
//
//  Created by FENGYL on 2017/5/17.
//  Copyright © 2017年 FENGYL. All rights reserved.
//

#import "TotalDeviceSelectedViewController.h"
#import "PullUpDeviceTableViewCell.h"
#import "UserModel.h"

@interface TotalDeviceSelectedViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *deviceSelectTableView;

@property (strong, nonatomic) UserModel *userModel;

@end

@implementation TotalDeviceSelectedViewController

-(UserModel *)userModel{
    if (!_userModel) {
        _userModel = [[UserModel alloc]init];
    }
    return _userModel;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainView" bundle:[NSBundle mainBundle]];
        
        self = [storyboard instantiateViewControllerWithIdentifier:@"TotalDeviceSelectedViewController"];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.deviceSelectTableView.delegate = self;
    self.deviceSelectTableView.dataSource = self;
    
    self.deviceSelectTableView.tableFooterView = [[UIView alloc]init];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.userModel.deviceByAccountArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dict = self.userModel.deviceByAccountArray[indexPath.row];
    
    PullUpDeviceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PullUpDeviceTableViewCell"];
    
    if (cell == NULL) {
        
        cell = [[PullUpDeviceTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PullUpDeviceTableViewCell"];
        
    }
    
    NSString *serialNumber = [dict valueForKey:NC_SERIAL_NUMBER];
    
    UIImage *image = [self.userModel.deviceIconDictionary valueForKey:serialNumber];
    
    if (!image) {
        
        image = [UIImage imageNamed:@"ic_tracker_useractivity_yuan_2"];
        
    }
    
    NSString *deviceName;
    
    if ([self.userModel.changeDeviceNameDictionary valueForKey:serialNumber]) {
        
        deviceName = [self.userModel.changeDeviceNameDictionary valueForKey:serialNumber];
        
    }else{
        
        deviceName = [dict valueForKey:NC_DISPLAY_NAME];
        
    }
    
    [cell setUpCellForDeviceIcon:image serialNumber: serialNumber deviceName:deviceName isSelect:![serialNumber isEqualToString:self.userModel.selectedDeviceSerialNumber]];
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PullUpDeviceTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    self.userModel.selectedDeviceSerialNumber = [cell getSerialNumberString];
    
    [self.deviceSelectTableView reloadData];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
